﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Model.Task;
using Backend.JYD.Common;

namespace Backend.JYD.EFDAL.DAL
{
    public class TaskStepInfoDAL : Backend.JYD.IDAL.Task.ITaskStepInfo
	{
		#region 查询数据TaskStepInfo

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<TaskStepInfo> GetTaskStepInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes;
				//其他条件代码
				return new PageInfo<TaskStepInfo>(page, query);
			}
		}
		/// <summary>
		/// 查询数据
		/// </summary>
		public TaskStepInfo GetTaskStepInfoByTaskDetailId(Int64 taskDetailId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes.Where(p => p.TaskDetailId == taskDetailId);
				return query.FirstOrDefault();
			}
		}

        public IList<TaskStepInfo> GetTaskStepInfoByTaskId(Int64 taskId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes.Where(p => p.TaskId == taskId && p.Status >= 0).OrderByDescending(p => p.TaskDetailId);
                return query == null ? null : query.ToList();
            }
        }

        public IList<TaskStepInfo> GetTaskFirstStepInfoByTaskIds(IList<Int64> taskIds)
        {
            Int64[] ids = taskIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes.Where(p => ids.Contains(p.TaskId) && p.StepId == 1);
                return query == null ? null : query.ToList();
            }
        }

        public PageInfo<TaskStepInfo> GetTaskStepInfoByEmpInfo(PageInfo page, Int64 roleId, Int64 empId, string status)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<TaskStepInfo> query = db.TaskStepInfoes;
                if (roleId > 0)
                {
                    string id = "," + roleId.ToString() + ",";
                    query = query.Where(p => ("," + p.RoleIds + ",").Contains(id));
                }
                if (empId > 0)
                {
                    string id = "," + empId.ToString() + ",";
                    query = query.Where(p => ("," + p.EmpIds + ",").Contains(id));
                }
                if (!string.IsNullOrWhiteSpace(status))
                {
                    string[] statuses = status.Split(',');
                    List<Int32> ids = new List<int>();
                    foreach (string s in statuses) { if (!string.IsNullOrWhiteSpace(s)) { ids.Add(NumberHelper.StrToInt(s, -1)); } }
                    Int32[] id = ids.ToArray();
                    query = query.Where(p => id.Contains(p.Status));
                }
                query = query.OrderByDescending(p => p.Status).ThenByDescending(p => p.CreateTime);
                //其他条件代码
                return new PageInfo<TaskStepInfo>(page, query);
            }
        }

		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据TaskStepInfo
		/// </summary>
		public Int64 AddTaskStepInfo(TaskStepInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.TaskStepInfoes.Add(info);
				db.SaveChanges();
				return info.TaskDetailId;
			}
		}
		#endregion

		#region 修改数据TaskStepInfo

		/// <summary>
		/// 根据字段（TaskDetailId）修改数据TaskStepInfo
		/// </summary>
		public bool UpdateTaskStepInfo(TaskStepInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				var query = db.TaskStepInfoes.Where(p => p.TaskDetailId == info.TaskDetailId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
                        temp.Remark = info.Remark;
                        temp.Status = info.Status;
                        //temp.RoleIds = info.RoleIds;
                        //temp.EmpIds = info.EmpIds;
                        temp.Type = info.Type;
                        
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion

	}
}